﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using MedicalCare.EntityDefinitions.Entities;
using MedicalCare.Messages;
using MedicalCare.Translator;
using NHibernate.Criterion;
using NHibernate.Transform;
using StructureMap;
using NHibernate;

namespace MedicalCare.DbLayer.Adaptors
{
    public class FamilyAdaptor : IFamilyAdaptor
    {
       private ISession _mcSession;

       public FamilyAdaptor()
        {
            _mcSession = ObjectFactory.GetNamedInstance<ISession>("MedicalCareSession");
        }

        ///////Dan 20120102
       public IList<FamilyEntity> GetFamily(string orderKey, string orderVal, string whereStr)
       {
           try
           {
               //var translator = ObjectFactory.GetInstance<IUserInfoToUserMessage>();
               var query = _mcSession.CreateSQLQuery(@" EXEC Family_Get @ORDERKEY=:ORDERKEY, @ORDERVAL=:ORDERVAL, @WHERE=:WHERE ")
                           .SetString("ORDERKEY", orderKey)
                           .SetString("ORDERVAL", orderVal)
                           .SetString("WHERE", whereStr)
                           .SetResultTransformer(Transformers.AliasToBean<FamilyEntity>());
               var familyList = query.List<FamilyEntity>();

               return familyList;
           }
           catch (Exception ex)
           {
               //return false;
           }
           return null;
       }
        //


       public int Update(FamilyMessage msg)
       {
           var result = default(int);
           try
           {
               if (msg.Id> 0)// update
               {
                   // TODO: 如果设备id大于0，说明设备已存在，则是更新设备信息
               }
               else // insert
               {
                   // TODO: 如果设备id为空/0，则是插入改设备
                   //Champion 2013/1/5
                   var query = _mcSession.CreateSQLQuery(@" EXEC Family_Insert @Id=:Id, @Age=:Age,@Name=:Name,@Gender=:Gender,@MedicalHistory=:MedicalHistory,@City=:City,@PostalCode=:PostalCode,@Province=:Province,@District=:District,@AddressDetail=:AddressDetail,@CreateUser=:CreateUser")
                               .SetInt32("Id", msg.Id)
                               .SetInt32("Age", msg.Age)
                               .SetString("Name", msg.Name)
                               .SetString("Gender", msg.Gender)
                               .SetString("MedicalHistory", msg.MedicalHistory)
                               .SetString("City", msg.FamilyAddress.City)
                               .SetString("PostalCode", msg.FamilyAddress.PostalCode)
                               .SetString("Province", msg.FamilyAddress.Province)
                               .SetString("AddressDetail", msg.FamilyAddress.AddressDetail)
                               .SetString("District", msg.FamilyAddress.District)
                               .SetString("CreateUser", msg.CreateUser)
                               .SetDateTime("ModifyDT", msg.ModifyDT)
                               .SetString("ModifyUser", msg.ModifyUser)
                               .SetResultTransformer(Transformers.AliasToBean<ResultEntity>());
                  var test = query.List<ResultEntity>();
                  result = test.FirstOrDefault().Id;

               }
           }
           catch (Exception ex)
           {
               result = -1;
           }

           return result;
       }

       public int InsertFamilyUserMapping(UserFamilyMappingMessage msg)
       {
           var result = default(int);
           try
           {
                   // TODO: 如果设备id为空/0，则是插入改设备
               result = _mcSession.CreateSQLQuery(@" EXEC User_family_Mapping_Insert @FamilyId=:FamilyId, @UserId=:UserId, @CreateUser=:CreateUser ")
                               .SetInt32("FamilyId", msg.FamilyId)
                               .SetInt32("UserId", msg.UserId)
                               .SetString("CreateUser", msg.CreateUser)
                               .ExecuteUpdate();
           }
           catch (Exception ex)
           {
               result = -1;
           }

           return result;
       }

       public int InsertEquipmentFamilyMapping(EquipmentFamilyMappingMessage msg)
       {
           var result = default(int);
           try
           {
               // TODO: 如果设备id为空/0，则是插入改设备
               result = _mcSession.CreateSQLQuery(@" EXEC Equipment_family_Mapping_Insert @EquipmentId=:EquipmentId, @FamilyId=:FamilyId, @CreateUser=:CreateUser ")
                               .SetInt32("EquipmentId", msg.EquipmentId)
                               .SetInt32("FamilyId", msg.FamilyId)
                               .SetString("CreateUser", msg.CreateUser)
                               .ExecuteUpdate();
           }
           catch (Exception ex)
           {
               result = -1;
           }

           return result;
       }

       public IList<FamilyEntity> GetPagingRecords(string orderKey, string orderVal,
          int pageIndex, int pageSize, string whereStr)
       {
           throw new NotImplementedException();
       }

       public void Remove(FamilyMessage message)
       {
           throw new NotImplementedException();
       }


       public FamilyMessage Read(string id)
       {
           throw new NotImplementedException();
       } 

       public int GetRecordsTotal(string whereStr)
       {
           try
           {
               //var translator = ObjectFactory.GetInstance<IUserInfoToUserMessage>();
               var query = _mcSession.CreateSQLQuery(@" EXEC Familys_Paging_Total @WHERE=:WHERE ")
                        .SetString("WHERE", whereStr)
                         .SetResultTransformer(Transformers.AliasToBean<TotalEntity>());

               var result = query.List<TotalEntity>().FirstOrDefault().Total;

               return result;
           }
           catch (Exception ex)
           {
               //return false;
           }
           return 0;
       }
    }
}
